package com.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.guli.common.utils.HttpUtils;
import com.guli.common.vo.WeiboAccessVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;

/**
 * @ClassName WebController
 * @Author Hao_YunKai
 * @Date 2022/2/8 16:34
 */
@Controller
public class WebController {

    @GetMapping(value = {"/","login"})
    public String login() {
        return "login";
    }

    @GetMapping(value = {"register"})
    public String register() {
        return "register";
    }

    // 微博登录成功回调接口
    @GetMapping("/oauth2.0/weibo/success")
    public String weiboSuccess(String code) throws Exception {
        System.out.println(code);

        // 换取access_token https://api.weibo.com/auth2/access_token
        HashMap<String, String> map = new HashMap<>();
        map.put("client_id","2128432522");
        map.put("client_secret","f270eebf4771d93c5bb26b2a2fd320cd");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("code",code);
        HttpResponse httpResponse = HttpUtils.doPost("https://api.weibo.com/oauth2/access_token", "", "post", null, map, "");
        String s = EntityUtils.toString(httpResponse.getEntity());
        WeiboAccessVo weiboAccessVo = JSON.parseObject(s, new TypeReference<WeiboAccessVo>() {
        });
        System.out.println(weiboAccessVo);

        // feign调用用户模块 完成注册或登录
        return "redirect:http://gulimall.com";
    }

}